package com.farah.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.farah.lease.model.entity.*;
import com.farah.lease.model.enums.ItemType;
import com.farah.lease.web.admin.mapper.*;
import com.farah.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farah.lease.web.admin.vo.attr.AttrValueVo;
import com.farah.lease.web.admin.vo.graph.GraphVo;
import com.farah.lease.web.admin.vo.room.RoomDetailVo;
import com.farah.lease.web.admin.vo.room.RoomItemVo;
import com.farah.lease.web.admin.vo.room.RoomQueryVo;
import com.farah.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 不爱跑步的法拉韶
 * @Date 2024-06-21__12:08
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {


    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityInfoService;
    @Autowired
    private RoomLabelService roomLabelService ;
    @Autowired
    private RoomPaymentTypeService paymentTypeService;
    @Autowired
    private RoomLeaseTermService leaseTermService;

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    /**
     * 保存或更新房间_信息
     * @param roomSubmitVo 房间_信息
     */
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        //判断是否有id
        boolean isUpdate = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);
        //若为更新
        if (isUpdate) {
            //1.删除图片列表graphInfoList
            LambdaUpdateWrapper<GraphInfo> graphInfoWrapper=new LambdaUpdateWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
            graphInfoService.remove(graphInfoWrapper);
            //2.删除属性值列表attrValueIds
            LambdaUpdateWrapper<RoomAttrValue> RoomAttrValueWrapper=new LambdaUpdateWrapper<>();
            RoomAttrValueWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
            roomAttrValueService.remove(RoomAttrValueWrapper);
            //3.删除配套信息列表facilityInfoIds
            LambdaUpdateWrapper<RoomFacility> facilityInfoWrapper=new LambdaUpdateWrapper<>();
            facilityInfoWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
            roomFacilityInfoService.remove(facilityInfoWrapper);
            //4.删除标签信息列表labelInfoIds
            LambdaUpdateWrapper<RoomLabel> labelInfoWrapper=new LambdaUpdateWrapper<>();
            labelInfoWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
            roomLabelService.remove(labelInfoWrapper);
            //5.删除支付类型列表paymentTypeIds
            LambdaUpdateWrapper<RoomPaymentType> paymentTypeWrapper=new LambdaUpdateWrapper<>();
            paymentTypeWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
            paymentTypeService.remove(paymentTypeWrapper);
            //6.删除可选租期列表leaseTermIds
            LambdaUpdateWrapper<RoomLeaseTerm> leaseTermWrapper=new LambdaUpdateWrapper<>();
            leaseTermWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
            leaseTermService.remove(leaseTermWrapper);
        }
        //分别保存
        //1.图片列表
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfos=new ArrayList<>();
            for(GraphVo graphVo:graphVoList){
                GraphInfo graphInfo =new GraphInfo();
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        //2.属性值列表
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            ArrayList<RoomAttrValue> roomAttrValues=new ArrayList<>();
            for(Long attrValueId:attrValueIds){
                RoomAttrValue roomAttrValue =RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
                roomAttrValue.setRoomId(roomSubmitVo.getId());
                roomAttrValue.setAttrValueId(attrValueId);
                roomAttrValues.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }
        //3.保存配套信息
        List<Long> facilityInfoIds=roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomFacilityList=new ArrayList<>();
            for(Long facilityInfoId:facilityInfoIds){
                RoomFacility roomFacility=RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityInfoService.saveBatch(roomFacilityList);
        }
        //4.保存标签列表
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if(!CollectionUtils.isEmpty(labelInfoIds)){
            List<RoomLabel> roomLabelList=new ArrayList<>();
            for(Long labelInfoId:labelInfoIds){
                RoomLabel roomLabel=RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //5.保存支付类型列表
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if(!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypeList=new ArrayList<>();
            for(Long paymentTypeId:paymentTypeIds){
                RoomPaymentType roomPaymentType=RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            paymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //6.保存可选租期列表
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm> roomLeaseTermList=new ArrayList<>();
            for(Long leaseTermId:leaseTermIds){
                RoomLeaseTerm roomLeaseTerm=RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            leaseTermService.saveBatch(roomLeaseTermList);
        }
    }

    /**
     * 根据条件分页查询房间列表
     * @param page 分页对象
     * @param queryVo 查询条件
     * @return 房间列表
     */
    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page,queryVo);
    }

    /**
     * 根据id获取房间详细信息
     * @param id 房间id
     * @return 房间详细信息
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //1.查询房间_信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //2.查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //3.查询图片信息
        List<GraphVo> graphVoList =  graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);
        //4.查询属性值信息
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectLIstByRoomId(id);
        //5.查询配套信息
        List<FacilityInfo> facilityInfoVoList = facilityInfoMapper.selectListByRoomId(id);
        //6.查询标签信息
        List<LabelInfo> labelInfoVoList = labelInfoMapper.selectListByRoomId(id);
        //7.查询支付类型信息
        List<PaymentType> paymentTypeVoList = paymentTypeMapper.selectListByRoomId(id);
        //8.查询可选租期信息
        List<LeaseTerm> leaseTermVoList = leaseTermMapper.selectListByRoomId(id);
        //传值
        roomDetailVo.setId(roomInfo.getId());
        roomDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        roomDetailVo.setRent(roomInfo.getRent());
        roomDetailVo.setIsRelease(roomInfo.getIsRelease());
        roomDetailVo.setApartmentId(roomInfo.getApartmentId());

        roomDetailVo.setApartmentInfo(apartmentInfo);

        roomDetailVo.setGraphVoList(graphVoList);

        roomDetailVo.setAttrValueVoList(attrValueVoList);

        roomDetailVo.setFacilityInfoList(facilityInfoVoList);

        roomDetailVo.setLabelInfoList(labelInfoVoList);

        roomDetailVo.setPaymentTypeList(paymentTypeVoList);

        roomDetailVo.setLeaseTermList(leaseTermVoList);
        return roomDetailVo;
    }

    /**
     * 根据公寓id查询房间列表
     * @param id 公寓id
     * @return 房间列表
     */
    @Override
    public List<RoomInfo> listBasicByApartmentId(Long id) {
        return roomInfoMapper.listBasicByApartmentId(id);
    }
}




